<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <object classid="clsid:5EB842AE-5C49-4FD8-8CE9-77D4AF9FD4FF" id="IdrControl1" width="0" height="0" >
        <%--如果需要控件图片不显示，请把上面的width和height改为0即可--%>
    </object>
	<title>图书借阅</title>
	<meta name="decorator" content="default"/>
	<link rel="stylesheet" href="${ctxStatic}/borrow/css/styles.css">
	<link rel="stylesheet" href="${ctxStatic}/borrow/css/styles-3.css">
	<script type="text/javascript">
		var index = 1;
		$(document).ready(function(){
			$('#rdSn').focus();
		});

		//添加
		function adddetail() {
			var hiddBanid=document.getElementById("hiddBanid").value;
			var banid=document.getElementById("banid").value;
			var isbn=document.getElementById("count").value;
			var bookname=document.getElementById("bookname").value;
			var classcode=document.getElementById("classcode").value;

			//为空的不能添加
			if(banid==null || banid==""){
                showTip("请选择或扫描书籍!");
				return 1;
			}

			//重复的不能添加
			var str="";
			$("#details input").each(function() {
				str += $(this).val()+","
			});

			str = str.substr(0,str.length-1);//除去最后一个“，”
			if(str.indexOf(banid)>=0){
                showTip("该书已经添加，请添加其他书籍!");
				return 1;
			}

			var html = "<tr id='tr_" + index + "'>" +
					"<td class='tit'><input name='hiddBanid' value='"+banid+"' type='hidden' readonly style='width: 100px;background-color: #eeeeee;'></td>" +
					"<td class='tit'>"+banid+"</td>" +
					"<td class='tit'>"+isbn+"</td>" +
					"<td class='tit'>"+bookname+"</td>" +
					"<td class='tit'>"+classcode+"</td>" +
					"<td class='tit'><a href='javascript:deleteBookInfo(\"tr_" + index + "\")'>删除</a></td></tr>";
			$("#details").append(html);
			index++;

			if(str.length==0){
				$("#banidlist").val(banid);//首次添加
			}{
				$("#banidlist").val(banid+","+str);//多行
			}
			$("#qq").val("");
			$("#qq").focus();
		}
		//添加
		function adddetailForAjax() {

			var hiddBanid=document.getElementById("hiddBanid").value;
			var banid=document.getElementById("banid").value;
			var isbn=document.getElementById("count").value;
			var bookname=document.getElementById("bookname").value;
			var classcode=document.getElementById("classcode").value;


			//重复的不能添加
			var str="";
			$("#details input").each(function() {
				str += $(this).val()+","
			});

			str = str.substr(0,str.length-1);//除去最后一个“，”
			if(str.indexOf(banid)>=0){
				return 1;

			}
			var html = "<tr id='tr_" + index + "'>" +
					"<td class='tit'><input name='hiddBanid' value='"+banid+"' type='hidden' readonly style='width: 100px;background-color: #eeeeee;'></td>" +
					"<td class='tit'>"+banid+"</td>" +
					"<td class='tit'>"+isbn+"</td>" +
					"<td class='tit'>"+bookname+"</td>" +
					"<td class='tit'>"+classcode+"</td>" +
					"<td class='tit'><a href='javascript:deleteBookInfo(\"tr_" + index + "\")'>删除</a></td></tr>";
			$("#details").append(html);
			index++;

			if(str.length==0){
				$("#banidlist").val(banid);//首次添加
			}
			$("#qq").val("");
			$("#qq").focus();
		}
		//删除
		function deleteBookInfo(id) {
			var  rmid = $("#"+id+" input").val();
			var bkIdlist= $("#banidlist").val().replace(rmid,"");//删除书籍ID，在此做一个替换
			$("#banidlist").val(bkIdlist);
			$("#" + id).remove();
		}

		//通过扫描枪扫描或手动输入按下回车直接查询读书证号获取用户信息
		function getReaderInfoBySn() {
			try{
				$.ajaxSetup({ cache: false });
				$.getJSON("${ctx}/book/reader/getReaderInfoBySn",{ readerCardNo: $("#rdSn").val()}, function (data) {
                    if (data != null && data != "") {
                        var status = "";
                        $("#rdId").val(data['rdId']);
                        $("#rdSn").val(data['rdCardno']);
                        $("#info_rdId").val(data['rdCardno']);
                        $("#info_rdName").val(data['rdName']);
                        if (data['rdState'] == 0) {
                            status = "正常";
                            $('#qq').focus();
                        } else if (data['rdState'] == 1) {
                            status = "停用";
                            showTip("该借书卡已经停用,请管理员重新激活!");
                            return;
                        } else {
                            status = "挂失";
                            showTip("该借书卡已经挂失,请管理员重新激活!");
                            return;
                        }
                        $("#info_rdState").val(status);
                        $("#info_rdRoleId").val(data['roler']['roleName']);
                        $("#info_rdTele").val(data['rdTele']);

                    } else {
                        showTip("该用户不存在！");
                    }
					$.ajaxSetup({ cache: true });
				});
			} catch(e){
                showTip(e)
			}
		}

		//通过扫描枪扫描或手动输入按下回车直接查询书籍编号获取书籍信息
		function getBookInfoByBanid() {
			var strBanid = $("#qq").val();
			var tempbanid="";
			var flag="false";
			try{
				$.ajaxSetup({ cache: false });
				$.getJSON("${ctx}/book/manage/getBookInfoBybandid",{ banid: strBanid}, function (data) {
                    if (data != null && data != "") {
                        if (data['isCount'] == "1") {
                            showTip("该书籍已经被借出！");
                            $("#qq").val("");
                        } else {
                            var tempBookname = data['bookname'];
                            if (data['othername'] != "" && data['othername'] != null) {
                                tempBookname = data['bookname'] + "-" + data['othername'];
                            }
                            $("#bkId").val(data['id']);
                            $("#banid").val(data['bookstore']['banid']);
                            $("#count").val(data['isbn']);
                            $("#bookname").val(tempBookname);
                            $("#classcode").val(data['classcode'] + "/" + data['bookstore']['place']);
                            tempbanid = data['bookstore']['banid'];
                            var storeplace = data['bookstore']['store'];
                            flag = "true";
                            submitBorrowBook($("#rdId").val(), tempbanid, storeplace);
                        }
                    } else {
                        showTip("该书籍不存在！");
                        $("#qq").val("");
                    }
				});
				$.ajaxSetup({ cache: true });
			} catch(e){
                showTip(e)
			}
			if(flag=="true"){}
		}

		function valuesClear(flag) {
			if(flag==1){
				$("#rdSn").val("");
			} else{
				$("#qq").val("");
			}

		}
		//手动按下按钮借出图书
        function checkdata() {
            var userinfo = $("#info_rdId").val();
            var bookinfo = $("#banidlist").val();
            var flag = 0;
            var arr = new Array();
            arr = bookinfo.split(",");
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != 0) {
                    flag = 1;
                    break;
                } else {
                    flag = 0;
                }
            }
            if ("" == userinfo || null == userinfo) {
                showTip("请选择用户!");
                return;
            } else if ("" == bookinfo || null == bookinfo || flag == 0) {
                showTip("请添加书籍!");
                return;
            } else {
                $("#inputForm").submit();
            }
        }
		//ajax调用直接提交借出图书
        function submitBorrowBook(rdid, banid, placeid) {
            if ("" == rdid || null == rdid) {
                showTip("请输入借书证号！");
                return;
            } else if ("" == banid || null == banid) {
                showTip("请选择图书！");
                return;
            } else {
                try {
                    $.getJSON("${ctx}/book/borrowed/borrow", {
                        rdid: rdid,
                        banid: banid,
                        placeid: placeid
                    }, function (data) {
                        if (data != null && data != "") {
                            if (data['retFlag'] == "NOPLACE") {
                                showTip("您不能借阅该书库的图书!");
                                resetValues();
                            } else if (data['retFlag'] == "STOPCARD") {
                                showTip("您的卡号已经停用~请重新激活!");
                                resetValues();
                            } else if (data['retFlag'] == "LOSECARD") {
                                showTip("您的卡号已经挂失~请重新激活");
                                resetValues();
                            } else if (data['retFlag'] == "OVERBORROW") {
                                showTip("您的借阅本书已经达到上限，不能借出!");
                                resetValues();
                            } else if (data['retFlag'] == "OVERDATE") {
                                showTip("您借阅的图书已经逾期未还，请还书后再借!");
                                resetValues();
                            } else if (data['retFlag'] == "SUCCESS") {
                                showTip("借出成功！");
                                resetValues();
                            }
                        } else {
                            showTip("书籍借出失败！");
                        }
                    });
                } catch (e) {
                    showTip(e)
                }
            }
        }
		function resetValues() {
			$("#qq").val("");
			$("#banid").val("");
			$("#count").val("");
			$("#bookname").val("");
			$("#classcode").val("");
			$("#details").html("");
		}

        function Idcard() {
            try {
                var ax = new ActiveXObject("IDRCONTROL.IdrControlCtrl.1");
            } catch (e) {
                alertx("控件未安装");
            }
            IdrControl1.RepeatRead(1);   //设置是否重复读卡  0-不重复  1-重复
            IdrControl1.setMute(0);   //设置是否静音读卡  0-不静音  1-静音

            //注意：第一个参数为对应的设备端口，USB型为1001，串口型为1至16
            var result = IdrControl1.ReadCard("1001", "");
            if (result === 1) {
                var idcardNo = IdrControl1.GetCode();
                $("#rdSn").val(idcardNo);
                getReaderInfoBySn();
            } else {
                if (result === -1) {
                    alertx("端口初始化失败！");
                }
                if (result === -2) {
                    alertx("请将身份证放到读卡器上！");
                }
                if (result === -3) {
                    alertx("读取数据失败！");
                }
            }
        }
	</script>
</head>
<body>

<form:form id="inputForm" modelAttribute="borrow" action="${ctx}/book/borrowed/save" method="post" class="form-horizontal">
	<form:hidden path="id"/>
	<input type="hidden" name="banidlist" id="banidlist"/>
	<sys:message content="${message}"/>
	<fieldset>
		<table class="table-form">
			<tr>
				<td ccolspan="1">借书证号</td>
				<td colspan="5">
					<input type="text" id="rdSn"  maxlength="50" onkeydown='if(event.keyCode==13){getReaderInfoBySn()}'
						   onfocus="valuesClear('1')"/>
                    &nbsp;<input type="button" value="读身份证" onClick="Idcard();">
                    &nbsp;扫描条码或者手动添加

					<%--<img src="<c:url value="/static/images/add.gif"/>" onclick="getinfo()" style="cursor: pointer;"/>&nbsp;&nbsp;&nbsp;--%>
					<form:input type="hidden" id="rdId" path="rdId" htmlEscape="false" maxlength="50"/>
				</td>
			</tr>

			<tr >
				<td class="tit" rowspan="2">读者信息</td>
				<td class="tit">借书证号</td>
				<td class="tit" >读者姓名</td>
				<td class="tit">联系电话</td>
				<td class="tit" >读者状态</td>
				<td class="tit">读者角色</td>
			</tr>
			<tr>
				<td class="tit" ><input type="text"  id="info_rdId"   maxlength="10" readonly style="background-color: #f3f3f3;width: 120px;"/></td>
				<td class="tit"><input type="text" id="info_rdName"  maxlength="10" readonly style="background-color: #f3f3f3;width: 120px;"/></td>
				<td class="tit" ><input type="text" id="info_rdTele"    maxlength="10" readonly style="background-color: #f3f3f3;width: 120px;"/></td>
				<td class="tit"><input type="text" id="info_rdState"    maxlength="10" readonly style="background-color: #f3f3f3;width: 120px;"/></td>
				<td class="tit" ><input type="text" id="info_rdRoleId"   maxlength="10" readonly style="background-color: #f3f3f3;width: 120px;"/></td>
			</tr>
			<tr>
				<td  colspan="1">图书条码</td>
				<td  colspan="5">
					<input type="text" id="qq"  maxlength="50"  onkeydown='if(event.keyCode==13){getBookInfoByBanid()}'  onfocus="valuesClear('1')"/>&nbsp;扫描条码或者手动添加
					<%--<img src="<c:url value="/static/images/add.gif"/>" onclick="getbookinfo()" style="cursor: pointer;"/>&nbsp;&nbsp;&nbsp;--%>
					<%--<input type="button" class="btn btn-primary" value="添加到借出列表"  onclick="adddetail()" id="addtolist" />--%>
				</td>
			</tr>
			<tr>
				<td class="tit" rowspan="2">书籍信息</td>
				<td class="tit" >图书条码</td>
				<td class="tit" >ISBN</td>
				<td class="tit" >图书名称</td>
				<td class="tit" >索书号</td>
			</tr>
			<tr id="bookinfo">
				<td class="tit" ><input type="text" id="banid"  maxlength="50" value="" readonly style="background-color: #f3f3f3;text-align: center"/></td>
				<td class="tit" ><input type="text" id="count"  maxlength="50" value="" readonly style="background-color: #f3f3f3;text-align: center"/></td>
				<td class="tit" ><input type="text" id="bookname"  maxlength="50" value="" readonly style="background-color: #f3f3f3;text-align: center"/></td>
				<td class="tit" ><input type="text" id="classcode"  maxlength="50" value="" readonly style="background-color: #f3f3f3;text-align: center"/></td>
				<input type="hidden" id="bkId" />
				<input type="hidden" id="hiddBanid" />
			</tr>
			<%--<tr>--%>
				<%--<td class="tit" colspan="1">借阅列表</td>--%>
				<%--<td class="tit"  colspan="5">--%>
					<%--<table id="contentTable" class="table table-striped table-bordered table-condensed">--%>
						<%--<thead>--%>
						<%--<tr>--%>
							<%--<th></th>--%>
							<%--<th>图书条码</th>--%>
							<%--<th>ISBN</th>--%>
							<%--<th>图书名称</th>--%>
							<%--<th>索书号</th>--%>
							<%--<th>操作</th>--%>
						<%--</tr>--%>
						<%--</thead>--%>
						<%--<tbody id="details">--%>
						<%--</tbody>--%>
					<%--</table>--%>
				<%--</td>--%>
			<%--</tr>--%>
		</table>
	</fieldset>
	<%--<div class="form-actions">--%>
		<%--<input id="btnSubmit" class="btn btn-primary" type="button" value="确认借出" onclick="checkdata()"/>--%>
		<%--<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>--%>
	<%--</div>--%>
</form:form>

</body>
</html>
